package com.wlhse.controller;

import com.wlhse.service.PlanService;
import com.wlhse.dto.inDto.PlanDto;

import com.wlhse.util.GetCurrentUserIdUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController("PlanController")
@RequestMapping("/api/v1")
//编制计划
public class PlanController {
    @Resource
    private PlanService planService;

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    @RequestMapping(value = "/plan", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String addPlan(@RequestBody(required = false) PlanDto planDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        planDto.setCheckPersonsID(userId);
        return planService.addPlan(planDto, userId);
    }

    @RequestMapping(value = "/plan/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnPlanById(@PathVariable int id, @ModelAttribute PlanDto planDto) {
        planDto.setPlanId(id);
        return planService.queryPlansByTaskID(planDto);
    }

    @RequestMapping(value = "/plan/tree/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnTree(@PathVariable int id) {
        return planService.getTreeDto(id);
    }

    @RequestMapping(value = "/plan/node/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnChooseNode(@PathVariable int id) {
        return planService.getChooseNodeID(id);
    }

    @RequestMapping(value = "/plan/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public String deleteTask(@PathVariable int id) {
        return planService.deletePlan(id);
    }

    @RequestMapping(value = "/plan/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String modifyPlan(@PathVariable("id") Integer id, @RequestBody(required = false) PlanDto planDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        planDto.setCheckPersonsID(userId);
        planDto.setPlanId(id);
        return planService.modifyPlan(planDto);
    }

    //查 by PlanID
    @RequestMapping(value = "/plan_detail/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryPlanByID(@PathVariable int id) {
        return planService.queryPlanByID(id);
    }

    //条件查询所有计划
    @RequestMapping(value = "/plan_detail", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryPlanByCondition(@ModelAttribute PlanDto planDto) {
        return planService.queryPlanByCondition(planDto);
    }

    //条件查询当前登录人计划
    @RequestMapping(value = "/current_person_plan", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryCurrentPerson(@ModelAttribute PlanDto planDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        planDto.setCheckPersonsID(userId);
        return planService.queryPlanByCondition(planDto);
    }

    //状态查询当前登录人计划
    @RequestMapping(value = "/current_plan", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryCurrentPersonByStatus(@ModelAttribute PlanDto planDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        planDto.setCheckPersonsID(userId);
        return planService.queryCurrentPersonByStatus(planDto);
    }

    @RequestMapping(value = "/plan_company/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getCompany(@PathVariable int id) {
        return planService.queryPlanCompany(id);
    }

}
